package com.java.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.java.domain.KsDo;
import com.java.service.KsService;
import com.java.utils.JsonUtils;
import com.java.utils.StrUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class KsManageAction extends ActionSupport implements ModelDriven<KsDo> {

	private static final long serialVersionUID = 12L;

	@Resource
	private KsService ksservice;

	public String showksManage() {
		return "showksManage";
	}

	public void showOne() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ksId = request.getParameter("ksId");
		KsDo ks = ksservice.getById(Integer.parseInt(ksId));
		JsonUtils.JsonTostring(ks);
	}

	/**
	 * 查询所有的科室
	 */
	public void showKsList() {
		List<KsDo> ksList = ksservice.findAll();
		JsonUtils.JsonTostring(ksList);
	}

	public void save() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String dname = request.getParameter("dname");
		String descript = request.getParameter("descript");
		Map<String, Object> map = new HashMap<String, Object>();
		if (StrUtils.isNotEmpty(dname) && StrUtils.isNotEmpty(descript)) {
			KsDo kDao = ksservice.queryByname(dname);
			if(kDao==null){
				KsDo ksdo = new KsDo();
				ksdo.setDname(dname);
				ksdo.setDescript(descript);
				ksservice.save(ksdo);
			}else{
				kDao.setDescript(descript);
				ksservice.update(kDao);
			}
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	/**
	 * 删除科室
	 */
	public void delete() {
		Map<String, Object> map = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		if (StrUtils.isNotEmpty(id)) {
			ksservice.delete(Integer.parseInt(id));
			map.put("success", "success");
		} else {
			map.put("error", "error");
		}
		JsonUtils.JsonTostring(map);
	}

	public KsDo getModel() {
		return null;
	}

}
